package cheme.ProcessEditor.Parts.Equations;

import java.beans.PropertyChangeEvent;
import java.util.List;

import org.eclipse.draw2d.FreeformFigure;
import org.eclipse.draw2d.FreeformViewport;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Viewport;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.GraphicalEditPart;

import cheme.ProcessEditor.ChemeConstants;
import cheme.ProcessEditor.LeafFigureNavigator;
import cheme.ProcessEditor.Figures.DecoratableFigure;
import cheme.ProcessEditor.Figures.EquationsFig;
import cheme.ProcessEditor.Figures.Util.IEquationPalette;
import cheme.ProcessEditor.Parts.INonResizablePart;
import cheme.ProcessEditor.Parts.PropertyAwarePart;
import cheme.ProcessModel.Equations.Equations;

public class PartEquations extends PropertyAwarePart implements  IEquationPalette ,INonResizablePart
{	
	@Override
	protected DecoratableFigure<EquationsFig> createFigure()
	{
		EquationsFig fig = new EquationsFig();
		return new DecoratableFigure<EquationsFig>(fig);
	}

	@Override
	protected void createEditPolicies()
	{
		installEditPolicy(EditPolicy.COMPONENT_ROLE, new ccc_aaa_PolicyEquations());
		installEditPolicy(EditPolicy.LAYOUT_ROLE, new PolicyEquationsLayout());
	}
	
	public DecoratableFigure<EquationsFig> getFigure()
	{
		return (DecoratableFigure<EquationsFig>)super.getFigure();
	}
	
	/*
	@Override
	protected void refreshVisuals()
	{
		
		Rectangle parentBounds = ((GraphicalEditPart)getParent()).getFigure().getBounds();
		
	    if(((GraphicalEditPart)getParent()).getFigure() instanceof FreeformFigure)
	    {
	    	parentBounds = ((FreeformFigure)((GraphicalEditPart)getParent()).getFigure()).getFreeformExtent();
	    }
	    
		try
		{
			Viewport vp = LeafFigureNavigator.getViewport(this);
			Rectangle bounds = vp.getBounds();
			
			if(vp instanceof FreeformViewport)
			{
				FreeformViewport ffvp = (FreeformViewport)vp;
				
				bounds = ffvp.getClientArea();
				ffvp.getClientArea(bounds);
				
				Dimension s = ffvp.getSize();
				s = ffvp.getPreferredSize();
				
				Point location = ffvp.getViewLocation();
				
				System.out.println(bounds);
			}
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		
		super.refreshVisuals();
	}
	@Override
	protected void addChildVisual(EditPart childEditPart, int index)
	{
		IFigure child = ((GraphicalEditPart)childEditPart).getFigure();
		IFigure layer = getLayer(ChemeConstants.CHEME_EQUATIONS_LAYER);
		layer.add(child);
	}
	*/
	
	public Equations getModel()
	{
		return (Equations)super.getModel();
	}

	@Override
	protected List getModelChildren()
	{
		return getModel().getChildren();
	}
	
	@Override
	public IFigure getContentPane()
	{
		return getFigure().getContentPane().getEquationsCompartment();
	}
		
	@Override
	protected void handleBoundsChange(PropertyChangeEvent evt)
	{
		refreshVisuals();
		super.handleBoundsChange(evt);
	}
	
	/**
	 * Reset the layout constraint, and revalidate the content pane
	 */
	protected void refreshVisuals()
	{
		Point loc = getModel().getLocation();
		Rectangle constraint = new Rectangle(loc.x, loc.y, -1, -1);
		
		((GraphicalEditPart)getParent()).setLayoutConstraint(this, getFigure(), constraint);
	}
	
	/*
	 * Upon deleteing the equaiton list from the equation editor, we want to select
	 * the editor so that a subsequent press of the enter keu will yield the creation 
	 * and addition of a new equation.
	 */
	@Override
	protected void removeChild(EditPart child)
	{
		super.removeChild(child);
		
		if(getChildren().size() == 1)
		{
			LeafFigureNavigator.navigateTo(this);
		}
	}
}
